package com.seahorse.youliao.controller;

import com.seahorse.youliao.vo.request.UserRequestVO;
import com.zengtengpeng.annotation.Lock;
import com.zengtengpeng.operation.RedissonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @ProjectName: youliao
 * @Package: com.seahorse.youliao.controller
 * @ClassName: RedissonObjectController
 * @Description: redisson 操作实列
 * @author:songqiang
 * @Date:2019-12-31 14:40
 **/
@Api(value = "RedissonObjectController", tags = "redisson 操作实列")
@Controller
@RequestMapping(value = "/redisson")
public class RedissonObjectController {


    private static Logger logger = LoggerFactory.getLogger(RedissonObjectController.class);

    @Resource
    private RedissonObject redissonObject;

    @Resource
    private RedissonClient redissonClient;


    /**
     * 设置用户信息值
     * @param user
     * @return
     */
    @ApiOperation(value = "设置用户信息值")
    @PostMapping("/setUser")
    @ResponseBody
    public String setUser(@RequestBody UserRequestVO user)  {

        if(user == null){
            UserRequestVO user1 = new UserRequestVO();
            user1.setName("test");
            user1.setAge("123");
            redissonObject.setValue("object1", user1,-1L);
        }
        redissonObject.setValue("object1", user,-1L);
        return "ok";
    }

    /**
     * 获取用户信息值
     * @return
     */
    @ApiOperation(value = "获取用户信息值")
    @PostMapping("/getUser")
    @ResponseBody
    public Object getUser() {


        Iterable<String> keys = redissonClient.getKeys().getKeysByPattern("*");
        for (String key : keys) {
            logger.info(key);
            Object o = redissonObject.getValue(key);
            logger.info("0={}",o);
        }
        return redissonObject.getValue("object1");
    }

    /**
     * 如果对象不存在则设置,否则不设置
     * @return
     */
    @ApiOperation(value = "如果对象不存在则设置,否则不设置")
    @PostMapping("/trySetValue")
    @ResponseBody
    public String trySetValue() {

        return redissonObject.trySetValue("object1","object1-2").toString();
    }


    /**
     * 1.支持spel表达式,keyConstant是常量(可选)
     */
    @Lock(keys = "#user.name",keyConstant = "常量")
    @ApiOperation(value = "分布式锁测定")
    @PostMapping("/testLock")
    @ResponseBody
    public String testLock(@RequestBody UserRequestVO user) {

        System.out.println("进来了lock test1"+user.toString());
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("出去了lock test2"+user.toString());
        return "lock test";
    }
}
